package com.elvt.bolg.filter;

import com.elvt.bolg.pojo.User;
import com.wf.captcha.ArithmeticCaptcha;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截所有资源
 * 对个别资源如登录和静态资源放行
 * 登录后放行资源
 */
@WebFilter("/*")
public class LoginAccessFilter  implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request= (HttpServletRequest) servletRequest;
            HttpServletResponse response= (HttpServletResponse) servletResponse;

            //获取访问的路径
        String path = request.getRequestURI();
        //指定页面放行，无需登录！  给登录页面放行
        if (path.contains("/login.jsp")){
            filterChain.doFilter(request,response);
            return;
        }
        //2、对静态资源放行
        if (path.contains("/static")){
            filterChain.doFilter(request,response);
            return;
        }

        if (path.contains("/user")){
            String actionName = request.getParameter("actionName");
            if ("login".equals(actionName)){
                filterChain.doFilter(request,response);
                return;
            }
        }
        if (path.contains("/captcha")){
            filterChain.doFilter(request,response);
            return;
        }
        //4、登录状态判断，session中有user对象就可以跳转到主页
        User user = (User) request.getSession().getAttribute("user");
        if (user!=null){
            filterChain.doFilter(request,response);
            return;
        }
        //免登录（自动登录）从cookie中拿到用户名密码数组
        Cookie[] cookies=request.getCookies();
        //判断cookie是否为空
        ServletOutputStream outputStream = response.getOutputStream();
        //算术验证码 数字加减乘除. 建议2位运算就行:captcha.setLen(2);
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);

        captcha.setLen(2);
        // 获取运算的结果
        String loginCodeRes = captcha.text();
        request.getSession().setAttribute("loginCodeRes",loginCodeRes);
        System.out.println("fff"+loginCodeRes);
        if (cookies!=null && cookies.length>0){
            for (Cookie cookie : cookies) {
                if ("user".equals(cookie.getName())){
                    String value=cookie.getValue(); //拿到数据 admin-xxx
                    String[] val=value.split("-");
                    String username=val[0];
                    String password=val[1];

                    String url="user?actionName=login&username="+username+"&password="+password+"&code="+loginCodeRes;
                    request.getRequestDispatcher(url).forward(request, response);
                    return;
                }
            }
        }

        //如果都没有走前面的判断直接重定向
        response.sendRedirect("login.jsp");

    }

    @Override
    public void destroy() {

    }
}
